package com.pw.demo3.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pw.demo3.annotation.PerAnnotion;
import com.pw.demo3.bean.Permission;
import com.pw.demo3.bean.Role;
import com.pw.demo3.bean.RolePermission;
import com.pw.demo3.service.impl.PermissionServiceImpl;
import com.pw.demo3.service.impl.RolePermissionServiceImpl;
import com.pw.demo3.service.impl.RoleServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/role")
public class roleController {

    @Autowired
    RoleServiceImpl roleService;

    @Autowired
    RolePermissionServiceImpl rolePermissionService;

    @Autowired
    PermissionServiceImpl permissionService;

    @RequestMapping("/delete/{id}")
    @ResponseBody
    @PerAnnotion(name = "角色删除",expression = "role:delete")
    public Map<String, Object> roleDelete(@PathVariable String id) {
        Map<String, Object> map = new HashMap<>();
        try {
            roleService.deleteByPrimaryKey(Long.valueOf(id));
            rolePermissionService.deleteById(Long.valueOf(id));
            map.put("success", true);
        } catch (Exception e) {
            map.put("success", false);
            map.put("msg", "错误");
            e.printStackTrace();
        }
        return map;
    }

    @RequestMapping("/list")
    @PerAnnotion(name = "角色列表",expression = "role:list")
    public String roleList(Model model,
                           String currentPage) {
        int pageNum = 1;
        if (currentPage != null) {
            pageNum = Integer.parseInt(currentPage);
        }
        PageHelper.startPage(pageNum, 5);
        List<Role> roleList=roleService.selectAll();
        PageInfo<Role> pageInfo=new PageInfo<>(roleList);
        model.addAttribute("pageInfo", pageInfo);
        return "views/role/list";
    }

    @RequestMapping("/input")
    @PerAnnotion(name = "角色新增或者删除操作",expression = "role:input")
    public String roleInput(Model model,
                            @RequestParam(name = "id", defaultValue = "0") String id){
        List<Permission> permissions=permissionService.selectAll();
        Role role=roleService.selectByPrimaryKey(Long.valueOf(id));
        model.addAttribute("role",role);
        model.addAttribute("permissions",permissions);
        if(!id.equals("0")){
            List<RolePermission> rolePermissions=rolePermissionService.selectById(Long.valueOf(id));
            List<Permission> selfPermissions=new ArrayList<>();
            for(RolePermission r:rolePermissions){
                selfPermissions.add(permissionService.selectByPrimaryKey(r.getPermissionId()));
            }
            model.addAttribute("selfPermissions",selfPermissions);
        }
        return "views/role/input";
    }

    @RequestMapping("/insert")
    public String insert(Role role,
                         Long[] select2) {
        if (role.getId() != null) {
            update(role,select2);
        } else {
            add(role,select2);
        }
        return "redirect:/role/list";
    }

    private void update(Role role, Long[] select2) {
        try {
            roleService.updateByPrimaryKey(role);
            rolePermissionService.deleteById(role.getId());
        }catch (Exception e){
            e.printStackTrace();
        }
        addRolwPermission(role,select2);
    }

    private void add(Role role, Long[] select2) {
        roleService.insert(role);
        addRolwPermission(role,select2);
    }

    private void addRolwPermission(Role role, Long[] select2){
        for(Long l:select2){
            RolePermission rolePermission=new RolePermission();
            rolePermission.setRoleId(role.getId());
            rolePermission.setPermissionId(l);
            try {
                rolePermissionService.insert(rolePermission);
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }
}
